package com.huluobo.a2202mvvm_im_douyindemo.mine;


import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.UpdateIconBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.LoginBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.VideoListBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.FragmentMineBinding;
import com.huluobo.a2202mvvm_im_douyindemo.db.AppDBUtils;
import com.huluobo.a2202mvvm_im_douyindemo.login.LoginActivity;
import com.huluobo.a2202mvvm_im_douyindemo.login.LoginUserBean;
import com.huluobo.a2202mvvm_im_douyindemo.mvpDemo.UserBean;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMFragment;
import com.huluobo.a2202mvvm_im_douyindemo.utils.ImageLoader;
import com.hyphenate.chat.EMClient;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.logoutSuccess.observe(this, s -> {
            ToastUtils.showLong(s);
            Intent intent = new Intent(requireContext(), LoginActivity.class);
            startActivity(intent);
            requireActivity().finish();
        });

        viewModel.logoutFailed.observe(this, ToastUtils::showLong);

        viewModel.getUserIconSuccess.observe(this, loginBean -> {
            ImageLoader.getInstance().loadImage(requireContext(), loginBean.getIcon(), dataBinding.mineUserIcon);
        });

        viewModel.getUserIconFailed.observe(this, s -> {
            Log.i("TagA", "查找用户信息失败" + s);
        });

        viewModel.updateUserIconSuccess.observe(this, updateIconBeanResult -> {
            ToastUtils.showLong(updateIconBeanResult.getMessage());
            if (updateIconBeanResult.getCode() == 200) {
                ImageLoader.getInstance().loadImage(requireContext(), updateIconBeanResult.getData(),
                        dataBinding.mineUserIcon);
            }
        });

        viewModel.updateUserIconFailed.observe(this, s -> {
            Log.i("TagA", "上传图片失败:" + s);
        });

    }

    @Override
    protected void initView() {
        dataBinding.mineLogout.setOnClickListener(v -> {
            viewModel.logout();
        });
        // EMClient.getInstance().getCurrentUser();
        //getString(R.string.logout) 拿到String这个xml中对应id的String
        //String.format 可以替换字符串中%s的元素,使用%s标记一个位置,可以通过format的第二个参数来替换
        String username = String.format(getString(R.string.logout), EMClient.getInstance().getCurrentUser());
        dataBinding.mineLogout.setText(username);

        String QRCodeName = EMClient.getInstance().getCurrentUser();
        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.weixin);
        Bitmap image = CodeUtils.createImage(QRCodeName, 400, 400, logo);
        dataBinding.mineQRCode.setImageBitmap(image);

        List<VideoListBeanResult.VideoListBean> videoListBeans =
                AppDBUtils.getInstance().getVideoDao().queryVideoInfo();
        dataBinding.mineText.setText(videoListBeans.toString());

//        LoginBeanResult.LoginBean loginBean =
//                AppDBUtils.getInstance().getUserDao().queryUserInfoByName(EMClient.getInstance().getCurrentUser());
        viewModel.getUserIcon(EMClient.getInstance().getCurrentUser());

        dataBinding.mineUserIcon.setOnClickListener(v -> {
            //打开图片选择器
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            //从Activity的回调中取出bundle
            Bundle bundle = data.getExtras();
            //从bundle中取出图片的Bean类
            PictureBean bean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            //Bean类中有图片的地址
            String filePath = bean.getPath();
            //发起上传请求
            viewModel.updateUserIcon(filePath);
        }
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}